/*
 * Block comments with details of changes
 */
public with sharing class QueryRepertoryController
{
    public RepertoryQuery__c  p {get;set;}//要查询的产品
    public User u ;
    public List<QueryBean>qbList{get;set;}//查询结果
    public Map<String,String>companyMap;
    public QueryRepertoryController(ApexPages.StandardController std)
    {
        p = new RepertoryQuery__c();

        qbList = new List<QueryBean>();
        u = [select Id,SubCompany__c from User where Id =:UserInfo.getUserId()];
        companyMap = new Map<String,String>();
        for(String str : System.Label.Corpcode.split(',',-1)){
            List<String>s = str.split('-',-1);
            if(s.size() > 1)companyMap.put(s[1],s[0]);
        }
    }
    
    public void Search(){
        if(p.Product__c != null){

            Product__c prod = [select NCCode__c,BatchNumber__c from Product__c where Id =:p.Product__c];

            Map<String,String> data = new Map<String,String>();
            if(u.SubCompany__c ==null){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
                                                           '请设置子公司!')); 
                return;
            }
            if(companyMap.containsKey(u.SubCompany__c)){
                data.put('corpcode',companyMap.get(u.SubCompany__c));

            }else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
                                                           '没有匹配到您的公司,请联系管理员!'));     
                return;

            }
            data.put('invcode',prod.NCCode__c);
            if(prod.BatchNumber__c !=null)data.put('vbatchcode',prod.BatchNumber__c);
            String params = JSON.serialize(data);
            HttpRequest req = new HttpRequest();

            req.setMethod('POST');
            req.setHeader('Content-Type','text/plain');
            req.setBody(params);
            String url = System.Label.queryRepertoryURL;
            req.setEndpoint(url);
            HttpResponse res = Utils.postRequest(req, 3);
            if (res.getStatusCode() == 200) {
                //System.debug(res.getBody());
                Map<String,Object> r = (Map<String,Object> ) JSON.deserializeUntyped(res.getBody());
                if(r.get('code') == 0 || r.get('code') == '0'){
                    Map<String,Object> rs = (Map<String,Object>) JSON.deserializeUntyped(((String)r.get('result')));
                    if( rs.get('result') == 1 ){
                        List<Object> datas = (List<Object>)rs.get('data');

                        for(Object ob :datas){
                            QueryBean rd = new QueryBean((Map<String,Object>)ob);
                            qbList.add(rd);
                        }
                    }

                }
            }
        }
    }
    public class QueryBean{
        public String corpcode {get;set;}
        public String storcode {get;set;}
        public String invcode {get;set;}
        public String invname {get;set;}
        public String invspec {get;set;}
        public String invtype {get;set;}
        public String invdm {get;set;}
        public String vbatchcode {get;set;}
        public String dproducedate {get;set;}
        public String dvalidate {get;set;}
        public String nonhandnum {get;set;}
        public String jldw {get;set;}
        public String pzwh {get;set;}
        public String nfreezenum {get;set;}
        public String corpname {get;set;}
        public String bodycode {get;set;}
        public String bodyname {get;set;}
        public String storname {get;set;}
        public String fjldw {get;set;}
        public String invpinpai {get;set;}
        public String prodarea {get;set;}
        public QueryBean(Map<String,Object> data){
            corpcode = String.valueOf(data.get('corpcode'));
            storcode = String.valueOf(data.get('storcode'));
            invcode = String.valueOf(data.get('invcode'));
            invname = String.valueOf(data.get('invname'));
            invspec = String.valueOf(data.get('invspec'));
            invtype = String.valueOf(data.get('invtype'));
            invdm = String.valueOf(data.get('invdm'));
            vbatchcode = String.valueOf(data.get('vbatchcode'));
            dproducedate = String.valueOf(data.get('dproducedate'));
            dvalidate = String.valueOf(data.get('dvalidate'));
            nonhandnum = String.valueOf(data.get('nonhandnum'));
            jldw = String.valueOf(data.get('jldw'));
            pzwh = String.valueOf(data.get('pzwh'));
            nfreezenum = String.valueOf(data.get('nfreezenum'));
            corpname = String.valueOf(data.get('corpname'));
            bodycode = String.valueOf(data.get('bodycode'));
            bodyname = String.valueOf(data.get('bodyname'));
            storname = String.valueOf(data.get('storname'));
            fjldw = String.valueOf(data.get('fjldw'));
            invpinpai = String.valueOf(data.get('invpinpai'));
            prodarea = String.valueOf(data.get('prodarea'));
        }
    }
}